# Maintainer: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: Jachym Barvinek <jachymb@gmail.com>
# Contributor: Thomas Dziedzic < gostrc at gmail >
# Contributor: John Proctor <jproctor@prium.net>
# Contributor: Juergen Hoetzel <juergen@archlinux.org>

pkgname=swi-prolog
pkgver=9.0.4
pkgrel=2
pkgdesc='Prolog environment'
arch=(loong64 x86_64)
url='https://www.swi-prolog.org/'
license=(GPL LGPL)
depends=(db5.3 gperftools libarchive libxcrypt libyaml qt5-base util-linux-libs)
makedepends=(cmake jdk-openjdk junit libjpeg libxft libxinerama libxpm ninja python-protobuf unixodbc)
optdepends=('java-runtime: for using Prolog from Java'
            'jdk-openjdk: for using Prolog from Java'
            'libjpeg: for using the pl2xpce library'
            'libxft: for using the pl2xpce library'
            'libxinerama: for using the pl2xpce library'
            'libxpm: for using the pl2xpce library'
            'unixodbc: for using the odbc4pl library')
source=("$pkgname-$pkgver.tar.gz::https://www.swi-prolog.org/download/stable/src/swipl-$pkgver.tar.gz"
        bdb_5.3.patch)
b2sums=('e0fed5e81658e85e5e502c0b7002a624a70c040d45543545a30eeede18a3a79e33ba548a0f00eb6b4d0a535dbb9d029d6880caa84cee85d39fe7bf996d8e1e9c'
        '37e14768ec59f9aee4363c5ffd63be8c50c04eecf269e52e8aadd5cadbc99f6286aa676b1145b1391490ab49072039d896eed588e934ba769d09c6bdb2d2222d')

prepare() {
  cd swipl-$pkgver
  patch -Np1 -i ../bdb_5.3.patch
}

build() {
  cmake \
    -B build \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=/usr \
    -D CMAKE_C_FLAGS="$CFLAGS -fPIC -ffile-prefix-map=$PWD= -w" \
    -D LIBEDIT_LIBRARIES=/usr/lib/libedit.so.0 \
    -D LIBEDIT_INCLUDE_DIR=/usr/include \
    -D BDB_INCLUDE_DIR=/usr/include/db5.3 \
    -G Ninja \
    -S swipl-$pkgver
  swipl-$pkgver/scripts/pgo-compile.sh
  ninja -C build
}

check() {
  ctest --test-dir build
}

package() {
  DESTDIR="$pkgdir" ninja -C build install
}

# getver: www.swi-prolog.org/download/stable
